package com.xugou.controller;

import com.xugou.pojo.Client;
import com.xugou.pojo.Cost;
import com.xugou.pojo.Order;
import com.xugou.service.ClientService;
import com.xugou.service.CostService;
import com.xugou.service.OrderService;
import com.xugou.utils.GetOrderCost;
import com.xugou.utils.UtilDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author xugou
 * @date 2022/6/13  19:37
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    //生成订单编号
    static long orderNums;
    //订单个数
    static long getOrderNums;
    //修改时的订单id
    static int updateOrderId;

    static private String msg = "";

    @Autowired
    @Qualifier("orderServiceImpl")
    private OrderService orderService;

    @Autowired
    @Qualifier("costServiceImpl")
    private CostService costService;

    @Autowired
    @Qualifier("clientServiceImpl")
    private ClientService clientService;



    //获取所有订单
    @RequestMapping("/allOrder")
    public String getAllOrder(Model model){

        //获取订单表基本信息
        List<Order> allOrders = orderService.getAllOrders();
        model.addAttribute("orderlist",allOrders);

        getOrderNums = orderService.getOrderNums();
        model.addAttribute("getOrderNums",getOrderNums);
        return "/order/order_index";
    }

    //删除订单
    @RequestMapping("/delOrder/{orderId}")
    public String delOrder(@PathVariable("orderId")int id, HttpServletRequest req){

        int i = costService.deleteCost(id);
        System.out.println(i);

        boolean deleteOrder = orderService.isDeleteOrder(id);
        return "redirect:/order/allOrder";
    }

    //跳转到新增订单界面
    @RequestMapping("/toAddOrder")
    public String toAddOrder(Model model){

        //提前生成订单编号
        orderNums = orderService.getOrderNums()+1000;
        model.addAttribute("id",orderNums);

        return "/order/addOrder";
    }

    //增加订单
    @RequestMapping("/addOrder")
    public String addOrder(String clientName,String clientAddress,String clientTel,String consigneeName,
                           String consigneeTel,String destination,String goodsName,String goodNums,String orderProduceTime,
                           String remark,String trideType ,String preStartTime,String preArriveTime){

        //获取前端的参数

        //通过客户姓名查找客户对象
        if (clientService.queryClientByName(clientName)==null){
            //没有该发件人,新增客户信息
            Client newClient = new Client();
            newClient.setClientName(clientName);
            newClient.setClientAddress(clientAddress);
            newClient.setClientTel(clientTel);

            List<Client> clientList = clientService.getClientList();
            int size = clientList.size();
            newClient.setClientId(size+1);
            boolean flag = clientService.addClientInfo(newClient);

        }

        Client client = clientService.queryClientByName(clientName);

        //生成订单对象
        Order order = new Order();
        order.setOrderId((int)orderNums);
        order.setClientId(client.getClientId());
        order.setTrideType(trideType);
        order.setPreStartTime(null);
        order.setPreArriveTime(null);

        String utilDate = UtilDate.getUtilDate(new Date());
        order.setOrderProduceTime(utilDate);



        order.setOrderIssend(false);
        order.setOrderReceive(false);
        order.setConsigneeName(consigneeName);
        order.setConsigneeTel(consigneeTel);
        order.setDestination(destination);
        order.setGoodsName(goodsName);
        order.setGoodsNums(goodNums);
        order.setRemark(remark);

        System.out.println(order);
        //放入订单的客户信息和空运费信息,
        order.setClient(client);
        order.setCost(null);
        //先生成订单
        boolean isAddOrder = orderService.isAddOrder(order);

        //生成订单费用
        Cost cost = GetOrderCost.getOrderCostInfo();
        cost.setOrderId((int)orderNums);
        cost.setOrderTime(new Date());
        costService.bindCostOfOrder(cost);
        //放入订单的运费信息
        order.setCost(cost);



        if (isAddOrder){
            return "redirect:/order/allOrder";
        }else {
            msg = "error";
            return "redirect:/order/toAddOrder";
        }


    }

    //跳转到修改界面
    @RequestMapping("/toUpdateOrder/{orderId}")
    public String toUpdateOrder(@PathVariable("orderId")int id,Model model){
        updateOrderId = id;
        Order upOrder = orderService.getOrderInfoById(updateOrderId);
        Integer clientId = upOrder.getClientId();
        Client client = clientService.queryClientById(clientId);

        model.addAttribute("upOrder",upOrder);
        model.addAttribute("client",client);


        return "/order/updateOrder";
    }

    //修改订单
    @RequestMapping("/updateOrder")
    public String updateOrder(String consigneeName,String consigneeTel,String destination,String goodsName,
                              String goodNums,String remark,String preStartTime,String preArriveTime,
                              String orderProduceTime,boolean orderIssend,boolean orderReceive){


        System.out.println(updateOrderId);


        Order newUpdateOrder = orderService.getOrderInfoById(updateOrderId);
        newUpdateOrder.setConsigneeName(consigneeName);
        newUpdateOrder.setOrderIssend(orderIssend);
        newUpdateOrder.setOrderReceive(orderReceive);
        newUpdateOrder.setPreStartTime(preStartTime);
        newUpdateOrder.setPreArriveTime(preArriveTime);
        newUpdateOrder.setConsigneeTel(consigneeTel);
        newUpdateOrder.setDestination(destination);
        newUpdateOrder.setGoodsName(goodsName);
        newUpdateOrder.setGoodsName(goodNums);
        newUpdateOrder.setRemark(remark);


        orderService.updateOrderInfo(newUpdateOrder);

        return "redirect:/order/allOrder";
    }
}
